const {Sequelize, Model} = require('sequelize')
const sequelize = require('../../core/db')
const bcrypt = require('bcryptjs')

class User extends Model{}

User.init({
  id: {
    type: Sequelize.INTEGER,
    primaryKey: true,
    autoIncrement: true
  } ,
  uname: Sequelize.STRING,
  type: {
    type: Sequelize.INTEGER,
    defaultValue: 2
  },
  password: {
    type: Sequelize.STRING,
    set(val) {
      const salt = bcrypt.genSaltSync(10)   // 10为一个常用的数字，表示加密等级
      const psw = bcrypt.hashSync(val, salt)
      this.setDataValue('password', psw)
    }
  },
  phone: {
    type:Sequelize.STRING,
    unique: true
  },
  status: {
    type: Sequelize.INTEGER,
    defaultValue: 1
  },
  create_time: {
    type: Sequelize.BIGINT(13),
    defaultValue: Date.now()
  },
  update_time: {
    type: Sequelize.BIGINT(13),
    defaultValue: Date.now()
  }
}, {
  sequelize,
  tableName: 'user'
})


module.exports = {
  User
}